$(document).ready(function () {
    $.getJSON('/parking/blacklisttotalpage', function (data) {
        $("#total").html(data);
        if (data == "0") {
            $("#current").html(0);
        } else {
            $("#current").html(1);
        }
    });
    if ($("#carId").val() == "") {
        list(1);
    }
});

//分页查所有
function list(current){
    $.getJSON('/parking/blacklisttotalpage', function (data) {
        $("#total").html(data);
    });
    $.getJSON('/parking/listblack', {"current": current}, function (data) {
        var options;
        if (data != null) {
            $("tbody").empty();
            for (var i = 0; i < data.length; i++) {
                options += "<tr>\n" +
                    "                <td style='display:table-cell; vertical-align:middle'>"+data[i].blacklistId+"</td>\n" +
                    "                    <td style='display:table-cell; vertical-align:middle'>"+data[i].blacklistCarId+"</td>\n" +
                    "                    <td style='display:table-cell; vertical-align:middle'>"+data[i].blacklistPrice+"</td>\n" +
                    "                    <td style='display:table-cell; vertical-align:middle'>"+data[i].blacklistCount+"</td>\n" +
                    "                    <td style='display:table-cell; vertical-align:middle'>"+data[i].blacklistTotalPrice+"</td>\n" +
                    "                    <td style='display:table-cell; vertical-align:middle'>"+data[i].blacklistOrderNo+"</td>\n" +
                    "                    <td style='display:table-cell; vertical-align:middle'><button class='label label-success' style=\"border: none;\" onclick='deleteblacklist("+data[i].blacklistId+")'> 移除  </button></td>\n" +
                    "                </tr>";
            }
            $("tbody").append(options);
        }
    });
}

//首页方法
function first(){
    $("#current").html(1);
    if($("#carId").val()=="") {
        list(1);
    }else{
        search(1);
    }
}

//尾页方法
function last(){
    if($("#carId").val()=="") {
        list($("#total").html());
    }else{
        search($("#total").html());
    }
    $("#current").html($("#total").html());
}

//下一页方法
function next(){
    if(Number($("#current").html())<Number($("#total").html())) {
        var x=Number($("#current").html()) + 1;
        if($("#carId").val()=="") {
            list(x);
        }else{
            search(x);
        }
        $("#current").html(x);
    }
}

//上一页方法
function previous(){
    if(Number($("#current").html())>1){
        var x=$("#current").html()-1;
        if($("#carId").val()=="") {
            list(x);
        }else{
            search(x);
        }
        $("#current").html(x);
    }
}

//模糊查询
function search(current) {
    if($("#carId").val()!=""){
        $.getJSON('/parking/searchblaclist', {"carId": $("#carId").val(),"current": current},
            function (data) {
                $("tbody").empty();
                for (var i=0;i<data.length;i++) {
                    $("tbody").append(insertTableRow(data[i]));
                }
            }
        );
        $.getJSON('/parking/blacklisttotalpage2',{"carId": $("#carId").val()}, function (data) {
            $("#total").html(data);
            if(data=="0"){
                $("#current").html(0);
            }
        });
    }else{
        $.getJSON('/parking/blacklisttotalpage', function (data) {
            $("#total").html(data);
            if(data=="0"){
                $("#current").html(0);
            }
        });

        list(1);

    }
    $("#current").html(1);

}

function insertTableRow(data) {
    return "<tr>\n" +
        "                <td style='display:table-cell; vertical-align:middle'>"+data.blacklistId+"</td>\n" +
        "                    <td style='display:table-cell; vertical-align:middle'>"+data.blacklistCarId+"</td>\n" +
        "                    <td style='display:table-cell; vertical-align:middle'>"+data.blacklistPrice+"</td>\n" +
        "                    <td style='display:table-cell; vertical-align:middle'>"+data.blacklistCount+"</td>\n" +
        "                    <td style='display:table-cell; vertical-align:middle'>"+data.blacklistTotalPrice+"</td>\n" +
        "                    <td style='display:table-cell; vertical-align:middle'>"+data.blacklistOrderNo+"</td>\n" +
        "                    <td style='display:table-cell; vertical-align:middle'><button class='label label-success' style=\"border: none;\" onclick='deleteblacklist("+data.blacklistId+")'> 移除  </button></td>\n" +
        "                </tr>";
 }

 //删除方法
function deleteblacklist(blacklistid) {
    var x;
    $.ajax({
        type: "GET",
        url: "/parking/blacklistDel",
        data: {blacklistId: blacklistid},
        dataType: "json",
        success: function (data) {
            if (data.delResult == "true") {//删除成功：移除删除行
                $.getJSON('/parking/blacklisttotalpage', function (data) {
                    $("#total").html(data);
                });
                if($("#carId").val()!=""){
                    x=$("#current").html();
                    search($("#current").html());
                    $("#current").html(x);
                }else{
                    list($("#current").html());
                }
            } else if (data.delResult == "false") {//删除失败
                alert("对不起，删除名单失败");
            } else if (data.delResult == "notexist") {
                alert("对不起，名单不存在");
            }
        },
        error: function (data) {
            alert("对不起，删除失败");

        }
    });
}

